package com.wangshaoyu.music_player.controller;

import com.wangshaoyu.music_player.data_object.AlbumDo;
import com.wangshaoyu.music_player.repository.AlbumRepo;
import com.wangshaoyu.music_player.repository.RelationRepo;
import com.wangshaoyu.music_player.session_object.UserSO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Controller
@RequestMapping("/studio/album")
public class StudioAlbumDoController {
    private final AlbumRepo albumRepo;
    private final RelationRepo relationRepo;

    @Autowired
    public StudioAlbumDoController(AlbumRepo albumRepo, RelationRepo relationRepo) {
        this.albumRepo = albumRepo;
        this.relationRepo = relationRepo;
    }

    @PostMapping("/create.do")
    public String create(HttpServletRequest request, String title, String cover) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            UserSO user = (UserSO) session.getAttribute("currentUser");
            if (user != null) {
                albumRepo.insert(user.uid, title, cover);

                return "redirect:/studio/album/list.html";
            }
        }

        log.error("用户未登录，无法操作");
        return "redirect:/studio/user/login.html";
    }

    @GetMapping("/publish.do")
    public String publish(HttpServletRequest request, int aid) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            UserSO user = (UserSO) session.getAttribute("currentUser");
            if (user != null) {
                AlbumDo albumDo = albumRepo.selectOneByAid(aid);
                if (albumDo == null) {
                    log.error("要发布的专辑不存在: aid = {}", aid);
                    return "redirect:/studio/album/list.html";
                }

                if (!albumDo.uid.equals(user.uid)) {
                    log.error("要发布的专辑不属于这个用户: aid = {}, album.uid = {}, currentUser.uid = {}", aid, albumDo.uid, user.uid);
                    return "redirect:/studio/album/list.html";
                }

                albumRepo.updateToPublishedByAid(aid);

                return "redirect:/studio/album/list.html";
            }
        }

        log.error("用户未登录，无法操作");
        return "redirect:/studio/user/login.html";
    }

    @GetMapping("/withdraw.do")
    public String withdraw(HttpServletRequest request, int aid) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            UserSO user = (UserSO) session.getAttribute("currentUser");
            if (user != null) {
                AlbumDo albumDo = albumRepo.selectOneByAid(aid);
                if (albumDo == null) {
                    log.error("要下线的专辑不存在: aid = {}", aid);
                    return "redirect:/studio/album/list.html";
                }

                if (!albumDo.uid.equals(user.uid)) {
                    log.error("要下线的专辑不属于这个用户: aid = {}, album.uid = {}, currentUser.uid = {}", aid, albumDo.uid, user.uid);
                    return "redirect:/studio/album/list.html";
                }

                albumRepo.updateToWithdrawnByAid(aid);

                return "redirect:/studio/album/list.html";
            }
        }

        log.error("用户未登录，无法操作");
        return "redirect:/studio/user/login.html";
    }

    @GetMapping("/unbind.do")
    public String unbind(HttpServletRequest request, int aid, int tid) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            UserSO user = (UserSO) session.getAttribute("currentUser");
            if (user != null) {
                AlbumDo albumDo = albumRepo.selectOneByAid(aid);
                if (albumDo == null) {
                    log.error("要下线的专辑不存在: aid = {}", aid);
                    return "redirect:/studio/album/list.html";
                }

                if (!albumDo.uid.equals(user.uid)) {
                    log.error("要下线的专辑不属于这个用户: aid = {}, album.uid = {}, currentUser.uid = {}", aid, albumDo.uid, user.uid);
                    return "redirect:/studio/album/list.html";
                }

                relationRepo.deleteByAidAndTid(aid, tid);

                return "redirect:/studio/album/relate.html?aid=" + aid;
            }
        }

        log.error("用户未登录，无法操作");
        return "redirect:/studio/user/login.html";
    }

    @PostMapping("/bind.do")
    public String bind(HttpServletRequest request, int aid, String[] tid) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            UserSO user = (UserSO) session.getAttribute("currentUser");
            if (user != null) {
                AlbumDo albumDo = albumRepo.selectOneByAid(aid);
                if (albumDo == null) {
                    log.error("要下线的专辑不存在: aid = {}", aid);
                    return "redirect:/studio/album/list.html";
                }

                if (!albumDo.uid.equals(user.uid)) {
                    log.error("要下线的专辑不属于这个用户: aid = {}, album.uid = {}, currentUser.uid = {}", aid, albumDo.uid, user.uid);
                    return "redirect:/studio/album/list.html";
                }

                List<Integer> tidList = Arrays.stream(tid).map(s -> Integer.parseInt(s.substring(1))).collect(Collectors.toList());
                if (!tidList.isEmpty()) {
                    relationRepo.insertBatch(aid, tidList);
                }

                return "redirect:/studio/album/relate.html?aid=" + aid;
            }
        }

        log.error("用户未登录，无法操作");
        return "redirect:/studio/user/login.html";
    }
}
